def solution(n: int, nums: list) -> int:
    # write code here
    if n < 3:
        return max(nums)
    nums_set = sorted(list(set(nums)), reverse=True)
    if len(nums_set) < 3:
        return nums_set[0]
    return nums_set[2]


if __name__ == '__main__':
    print(solution(3, [3, 2, 1]) == 1)
    print(solution(2, [1, 2]) == 2)
    print(solution(4, [2, 2, 3, 1]) == 1)